<!--
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 14:41:20
 * @LastEditTime: 2023-10-26 15:28:26
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description:   :gridOps="gridOptions" other
-->
<template>
	<el-main style="height: 100%">
		<el-row style="height: 100%">
			<el-col :span="12" style="height: 100%">
				<sc-VxeTable ref="xGrid" :gridEvts="gridEvents" :checkMethod="visibleMethod" :tablecode="'pro_plan_dm'"
					:url="api.pro.wip.list" :params="{
						faultle_quantity: { type: 'gt', value: 0 },
						'process.name': '烘干',
					}">
				</sc-VxeTable>
			</el-col>
			<el-col :span="12" style="height: 100%">
				<el-form :model="form" :rules="rules" ref="dialogForm" label-width="70px" label-position="right"
					class="el-tabs--border-card" style="border: 1px solid #dedfe0">
					<el-row>
						<el-col :span="8">
							<el-form-item label="派车单" prop="car_id">
								<skAutoChoose v-model="form.car.code" :name="'car'" :assign="form" @select="selectCar">
								</skAutoChoose>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item label="运输商" prop="car.company_id">
								<skAutoChoose v-model="form.car.company.name" :name="'factory'" :asKey="'company_id'" :assign="form.car"
									:params="[{ category_id: 54 }]">
								</skAutoChoose>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item label="运费" prop="car.amount">
								<el-input v-model="form.car.amount" clearable></el-input>
							</el-form-item>
						</el-col>

						<el-col :span="8">
							<el-form-item label="搬运单" prop="carry_id">
								<skAutoChoose v-model="form.carry.code" :name="'carry'" :assign="form"
									@select="selectCarry">
								</skAutoChoose>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item label="搬运人" prop="carry.company_id">
								<skAutoChoose v-model="form.carry.company.name" :name="'factory'" :asKey="'company_id'" :assign="form.carry"
									:params="[{ category_id: 55 }]">
								</skAutoChoose>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item label="搬运费" prop="carry.amount">
								<el-input v-model="form.carry.amount" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :lg="12">
							<el-row>
								<el-col :span="12">
									<el-form-item label="门市部" prop="ratio">
										<el-input v-model="form.department.name" disabled />
									</el-form-item>
								</el-col>
								<el-col :span="12">
									<el-form-item label="客户">
										<el-input v-model="form.company.name" disabled />
									</el-form-item>
								</el-col>
								<el-col :span="12">
									<el-form-item label="加工厂" prop="outfactory.name">
										<skAutoChoose v-model="form.outfactory.name" :params="[{ category_id: 77 }]"
											:name="'factory'" :assign="form" :asKey="'outfactory_id'"
											@select="outfactorySelect">
										</skAutoChoose>
									</el-form-item>
								</el-col>
								<el-col :span="12">
									<el-form-item label="收货部门">
										<skDicSelect :code="'defaultLocation'" v-model="form.location_id"
											:editsource="'default'">
										</skDicSelect>
									</el-form-item>
								</el-col>
								<el-col :span="12">
									<el-form-item label="正品数" prop="faultle_quantity">
										<el-input v-model="wip.faultle_quantity" disabled>
											<template #prepend><span style="width: 40px">{{
												wip.auxiliary_quantity
											}}</span></template>
										</el-input>
									</el-form-item>
								</el-col>
								<el-col :span="12">
									<el-form-item label="次品数" prop="rejects_quantity">
										<el-input v-model="form.rejects_quantity" disabled />
									</el-form-item>
								</el-col>
								<el-col :span="12">
									<el-form-item label="倒毛包数" prop="plan_auxiliary_quantity">
										<el-input v-model="form.plan_auxiliary_quantity
											" @keyup="
		form.plan_quantity =
		form.plan_auxiliary_quantity *
		25
		" />
									</el-form-item>
								</el-col>
								<el-col :span="12">
									<el-form-item label="倒毛数量" prop="plan_quantity">
										<el-input v-model="form.plan_quantity" />
									</el-form-item>
								</el-col>

								<el-col :span="12">
									<el-form-item label=" 成品">
										<el-input v-model="form.material.name" disabled />
									</el-form-item>
								</el-col>
								<el-col :span="12">
									<el-form-item label="成品形态">
										<el-input v-model="form.type.name" disabled />
									</el-form-item>
								</el-col>
								<el-col :span="12">
									<el-form-item label="色号">
										<el-input v-model="form.color.code" disabled />
									</el-form-item>
								</el-col>
								<el-col :span="12">
									<el-form-item label="色名">
										<el-input v-model="form.color.name" disabled />
									</el-form-item>
								</el-col>
								<el-col :span="12">
									<el-form-item label="缸号">
										<el-input v-model="form.dyelot" disabled />
									</el-form-item>
								</el-col>
								<el-col :span="12">
									<el-form-item label="批号" prop="lot_name">
										<el-input v-model="form.lot_name" disabled />
									</el-form-item>
								</el-col>
								<el-col :span="24">
									<el-form-item label="备注" prop="remark">
										<el-input v-model="form.remark" type="textarea" maxlength="120" show-word-limit
											disabled />
									</el-form-item>
								</el-col>
							</el-row>
						</el-col>
						<el-col :lg="12">
							<el-form-item label="附件" prop="attachments">
								<sk-upload v-model="form.attachments" :limit="10" draggable tip="上传图片">
								</sk-upload>
							</el-form-item>
						</el-col>
						<el-col :span="24">
							<el-form-item>
								<el-button type="primary" :loading="loading" :disabled="!selected" @click="submit()">保
									存</el-button>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</el-col>
			<SaveDialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false">
			</SaveDialog>
		</el-row>
	</el-main>
</template>
<script>
export default {
	name: "pro_plan_dm",
};
</script>
<script setup>
import { ref } from "vue";
import SaveDialog from "./save";
import skAutoChoose from "@/components/skAutoChoose";
import skDicSelect from "@/components/skDicSelect";
import { showLoading, hideLoading } from "@/utils/showloading";
import api from "@/api";
const dialog = ref({
	save: false,
});
const emit = defineEmits(["success"]);
const xGrid = ref({});
const dialogForm = ref({});
const loading = ref(false);
const selected = ref(false);
const visibleMethod = ({ row }) => row.process_id == 6;

const form = ref({
	attachments: [],
	company: {},
	department: {},
	department_id: 0,
	material: { spec: "", name: "" },
	type: { name: "色筒" },
	color: {},
	outfactory_id: 52,
	outfactory: { name: "本厂倒毛(2楼)" },
	location_id: 1146,
	car: { company: {} },
	carry: { company: {} },
});
const rules = {};
const saveDialog = ref({});
const handleSuccess = () => {
	emit("success");
	xGrid.value.refresh();
};
const outfactorySelect = () => { };
//表单提交方法
const submit = () => {
	dialogForm.value.validate(async (valid) => {
		if (valid) {
			showLoading("保存中！");
			loading.value = true;

			api.pro.plan.savePlanSwip
				.post(form.value)
				.then((res) => {
					xGrid.value.refresh();
				})
				.finally(() => {
					hideLoading();
					loading.value = false;
				});
		}
	});
};
const selectCarry = (data) => {
	if (typeof data.code == "undefined") {
		data = { company: {} };
	}
	form.value.carry = data;
	form.value.carry_id = 0;
};
const selectCar = (data) => {
	if (typeof data.code == "undefined") {
		data = { company: {} };
	}
	form.value.car = data;
	form.value.car_id = 0;
};
const wip = ref({});
const gridEvents = {
	selectData(selectRecords) {
		if (selectRecords.length > 0) {
			// form.value = selectRecords[0];
			wip.value = selectRecords[0];
			let plan = wip.value["pro_plan"];

			delete plan.location_id;
			delete plan.outfactory_id;
			plan = Object.assign(form.value, plan);
			plan.plan_auxiliary_quantity =
				wip.value.faultle_auxiliary_quantity ??
				wip.value.faultle_quantity / 25;
			plan.plan_quantity = wip.value.faultle_quantity;

			plan.plan_id = 2; //倒毛
			selected.value = true;
			plan.material = wip.value.material;
			plan.color = wip.value.color;
			plan.company = wip.value.company;
			plan.wip_id = [wip.value.id];
			plan.pro_dept_id = 0;
			plan.id = 0;
		} else selected.value = false;
	},
};
</script>
